using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace Test
{
    public partial class PageLayout : Form
    {
        public PageLayout()
        {
            InitializeComponent();
            string path = openFileDialog1.FileName; ;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            txtNewPage.Enabled = false;
            checkBox1.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            textBox1.Clear();
            checkBox1.Enabled = false;
            txtNewPage.Enabled = false;

            System.Windows.Forms.DialogResult OPEN = openFileDialog1.ShowDialog();
            if (OPEN == DialogResult.OK)
            {

                    string path = openFileDialog1.FileName; // The Path to the .Xml file //  


                    FileStream READER = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //Set up the filestream (READER) //                
                    System.Xml.XmlDocument PageLayoutDoc = new System.Xml.XmlDocument();// Set up the XmlDocument (CompSpecs) //
                    textBox3.Text = path;
                    try
                    {
                        PageLayoutDoc.Load(READER); //Load the data from the file into the XmlDocument (CompSpecs) //
                        txtNewPage.Enabled = true;
                        button2.Enabled = true;
                        checkBox1.Enabled = true;
                        System.Xml.XmlNodeList lstPages = PageLayoutDoc.GetElementsByTagName("Page"); // Create a list of the nodes in the xml file //
                        textBox1.Text = lstPages.Count.ToString();
                        listBox1.Items.Clear();
                        for (int i = 0; i < lstPages.Count; i++)
                        {
                            string myval = lstPages[i].Attributes["PageSize"].InnerText + " L:" + lstPages[i].Attributes["PaddingLeft"].InnerText + " R:" + lstPages[i].Attributes["PaddingRight"].InnerText + " T:" + lstPages[i].Attributes["PaddingTop"].InnerText + " B:" + lstPages[i].Attributes["PaddingBottom"].InnerText;
                            listBox1.Items.Add(myval);
                        }
                    }
                catch(System.Xml.XmlException mye)
                    {
                        textBox3.Text = "Not a valid Flare/Blaze Page Layout(*.flpgl) file. Choose another one...";
                  

                }
                
 
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
         
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            String path = openFileDialog1.FileName;
            FileStream Reader = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            System.Xml.XmlDocument PageLayoutDoc = new System.Xml.XmlDocument();
            PageLayoutDoc.Load(Reader);

            FileStream Writer = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);


            System.Xml.XmlNodeList lstPages = PageLayoutDoc.GetElementsByTagName("Page");

            for (int i = 0; i < lstPages.Count; i++)
            {
                lstPages[i].Attributes["PageSize"].Value = txtNewPage.Text;

                if (checkBox1.Checked == true)
                {
                    lstPages[i].Attributes["PaddingLeft"].Value = txtBoxPadLeft.Text + comboBoxUnit.Text;
                    lstPages[i].Attributes["PaddingRight"].Value = txtBoxPadRight.Text + comboBoxUnit.Text;
                    lstPages[i].Attributes["PaddingTop"].Value = txtBoxPadTop.Text + comboBoxUnit.Text;
                    lstPages[i].Attributes["PaddingBottom"].Value = txtBoxPadBottom.Text + comboBoxUnit.Text;
                }
            }

            PageLayoutDoc.Save(Writer);

            listBox1.Items.Clear();
            for (int i = 0; i < lstPages.Count; i++)
            {
                string myval = lstPages[i].Attributes["PageSize"].InnerText + " L:" + lstPages[i].Attributes["PaddingLeft"].InnerText + " R:" + lstPages[i].Attributes["PaddingRight"].InnerText + " T:" + lstPages[i].Attributes["PaddingTop"].InnerText + " B:" + lstPages[i].Attributes["PaddingBottom"].InnerText;
                listBox1.Items.Add(myval);
            }
            Writer.Close();


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                groupBox1.Enabled = true;
            }
            else
                groupBox1.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }


    }
}